<template>
    <exPubTable ref="storeTable" :cfg.sync="storecfg" :default-sort="{prop: 'day', order: 'descending'}" :exData="storeList" @getDataList="queryStoreInfo"  :defPageSize="100" :maxHeight="400" :show-summary="true"></exPubTable>
</template>


<script>

export default {
    props:["qry"],
    data() {
        var that =this;
        return {
            totalMoney: 0,
            storeList:[],
            stockMap: {},
            storecfg:{
                showIndex: true,
                usePage:true,
                columns: [
                    {prop:"",label:"当前持仓",align:"center",
                        columns:[
                            {prop:"id",label:"ID",width:60},
                            {prop:"day",label:"日期",width:80,sortable:true},
                            {prop:"code",label:"代码",width:80,sortable:true},
                            {prop:"name",label:"名称",width:80},
                            {prop:"amount",label:"数量",width:60,formatter:this.bigMoneyFormat},
                            {prop:"cbj",label:"成本价",width:80,formatter:(row,col,val,idx)=>{
                                if(isNaN(val)){
                                    return val;
                                }else{
                                    return that.bigMoneyFormat(val)
                                }
                            }},
                            {prop:"cbfd",label:"成本浮动",width:80,sumType:"add",formatter:this.bigMoneyFixFormat,getColor(s){return that.getRateColor(s.row.cbfd)}},
                            {prop:"curprice",label:"当前价",width:70,formatter:this.bigMoneyFormat,getColor(s){return that.getRateColor(s.row.fdyk)}},
                            {prop:"zxsz",label:"最新市值",width:80,formatter:this.bigMoneyFormat,getColor(s){return that.getRateColor(s.row.fdyk)}},
                            {prop:"preZxsz",label:"市值浮动",width:80,getColor(s){return that.getRateColor(s.row.preZxsz)}},
                            {prop:"fdyk",label:"浮动盈亏",width:100,formatter:this.bigMoneyFixFormat0,sortable:true,getColor(s){return that.getRateColor(s.row.fdyk)}},
                            {prop:"rate",label:"盈亏比例",width:85,endStr:"%",formatter:this.bigMoneyFormat,getColor(s){return that.getRateColor(s.row.fdyk)}},
                            {prop:"",label:"今日",align:"center",
                                columns:[
                                    {prop:"zxjg",label:"当前价",width:70,getColor(s){return that.getRateColor(s.row.diff)}},
                                    {prop:"diff",label:"幅度",width:70,formatter:this.fixed2,getColor(s){return that.getRateColor(s.row.diff)}},
                                    {prop:"wave",label:"比例",width:85,endStr:"%",formatter:this.fixed2,getColor(s){return that.getRateColor(s.row.diff)}},
                                    {prop:"jryk",label:"盈亏",width:70,formatter:this.bigMoneyFormat,getColor(s){return that.getRateColor(s.row.diff)}},
                                ]
                            },
                            {prop:"createtime",label:"创建日期",width:150,sortable:true},
                        ]
                    }
                ],
            },
        }
    },

    mounted() {
    },
    inject:["getQueryInfo"],

    methods: {
        reQuery(){
            if(this.$refs.storeTable) this.$refs.storeTable.reQuery();
        },
        queryStoreInfo(pInfo){
            var opername = "queryStoreInfo";
            this.stockMap = {};
            this.registerListen(this,opername,(res)=>{
                if(res.succ){
                    pInfo.total = res.data.total;
                    this.storeList = res.data.records;
                    this.storeList.map((item,idx)=>{
                        item.createtime = this.$dayjs(item.createtime).format("YYYY-MM-DD HH:mm:ss");
                        if(!this.stockMap[item.code]){
                            item.zxjg = 0;
                            item.diff = 0;
                            item.wave = 0;
                            item.jryk = 0;
                            this.queryCurPrice(item);
                            this.stockMap[item.code] = true;
                        }
                        
                        var preItem = this.storeList[idx+1];
                        if(preItem && this.qry.code && preItem.code==item.code){
                            item.preZxsz =( item.zxsz-preItem.zxsz).toFixed(2)*1;
                            if(item.amount != preItem.amount){
                                var num = item.amount - preItem.amount;
                                item.preZxsz = item.preZxsz - (num*preItem.curprice);
                                var tmpcbj = item.cbj;
                                item.cbj = item.cbj - (item.curprice - preItem.cbj)/(item.amount/100);
                                item.cbj = item.cbj.toFixed(3)*1;

                                item.cbfd =( preItem.cbj-item.cbj)*item.amount;
                                item.cbfd = item.cbfd.toFixed(2)*1;
                                item.preZxsz = item.preZxsz.toFixed(2)*1;
                                
                                item.preZxsz = `<span title="显示的为数量变化修正后的值" class="blue">${item.preZxsz}</span>`
                                item.cbj = `<span title="显示的为数量变化修正后的值,原值:${tmpcbj}" class="blue">${item.cbj}</span>`
                            }else{
                                item.cbfd =( preItem.cbj-item.cbj)*item.amount;
                                item.cbfd = item.cbfd.toFixed(2)*1;
                            }
                        }else{
                            item.preZxsz = 0;
                            item.cbfd = 0;
                        }
                    });
                }else{
                    this.$message.error("查询异常:"+res.err)
                }
            });
            
            var arg = this.getQueryInfo();
            var param = {type:"commonQry",name:opername,table:"store",objInfo:arg};
            param.order = "code asc,day desc,zxsz desc";
            param.count = true;
            if(pInfo){
                param.pInfo=`${pInfo.page},${pInfo.size}`
            }
            electron.ipcRenderer.send("doSql",param)
        },
        queryCurPrice(item){
            this.$axios.get(`https://hsmarket.eastmoney.com/api/SHSZQuoteSnapshot?callback=&id=${item.code}`)
            .then((res) => {
                res.data = res.data.substring(1,res.data.length-2);
                res.data = JSON.parse(res.data);
                if (res.data.status == 0) {
                    var data = res.data;
                    var prePrice = data.fivequote.yesClosePrice;
                    this.storeList.map((row)=>{
                        row.jryk = row.jryk || 0;
                        if(row.code == item.code){
                            row.zxjg = data.realtimequote.currentPrice;
                            row.diff = data.realtimequote.currentPrice - data.fivequote.yesClosePrice;
                            row.wave = this.getRise(data.realtimequote.currentPrice,data.fivequote.yesClosePrice);
                            row.jryk = (row.amount * row.diff).toFixed(2) * 1;
                        }
                    });
                    this.storeList.push({});
                    this.storeList.pop();
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
    }
    
}
</script>

<style lang="less">

</style>
